//
//  CancelReason.m
//  YaoHeAPP
//
//  Created by mac on 15/12/21.
//  Copyright © 2015年 mac. All rights reserved.
//

#import "CancelReason.h"
#import "Controller.h"
#import "Define.h"
@implementation CancelReason

#define App_Frame_Height        [UIScreen mainScreen].bounds.size.height
#define App_Frame_Width         [[UIScreen mainScreen] bounds].size.width
#define Line_Low_Color   [UIColor colorWithRed:220.0/255.0 green:220.0/255.0 blue:220.0/255.0 alpha:1]
//红色
#define  RedColor  [UIColor colorWithRed:216.0/255.0 green:12.0/255.0 blue:24.0/255.0 alpha:1]



+(void)showPickerView:(UIView *)selfView withTitle:(NSString*)titleString oderID:(NSString*)oID andSelectBlock:(void (^)(NSString *))selectRow
{
    //创建pickerView
    CancelReason*cancel=[[CancelReason alloc]initWithFrame:selfView.bounds];
    cancel.SelectBlock=selectRow;
    cancel.titleStr=titleString;
    cancel.idStr=oID;
    [cancel setSubviews];
    [selfView addSubview:cancel];
    
}
//初始化大小
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
    }
    self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.3];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(removeSelfAction)];
    tap.delegate = self;
    [self addGestureRecognizer:tap];
    
    [self performSelector:@selector(showSelfAction) withObject:nil afterDelay:0];
    return self;
}

//设置，要弹出的视图上显示的东西
-(void)setSubviews
{
    hiddenYN=YES;
    _bgView = [[UIView alloc] initWithFrame:CGRectMake(20,350, App_Frame_Width-40,356)];
    _bgView.backgroundColor =[UIColor whiteColor];
    _bgView.layer.shadowColor = [UIColor blackColor].CGColor;
    _bgView.layer.shadowOpacity = .3;
    _bgView.layer.shadowOffset = CGSizeMake(0, -1);
    _bgView.layer.masksToBounds=YES;
    _bgView.layer.cornerRadius=10;
    _bgView.layer.borderWidth=0.5;
    _bgView.layer.borderColor=[UIColor lightGrayColor].CGColor;
    [self addSubview:_bgView];
    //标签
    UILabel *titleL = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, _bgView.frame.size.width, 39)];
    titleL.backgroundColor = [UIColor whiteColor];
    titleL.font = [UIFont systemFontOfSize:17];
    //titleL.textAlignment=NSTextAlignmentCenter;
    //titleL.textColor = [UIColor whiteColor];
    titleL.text = self.titleStr;
    [_bgView addSubview:titleL];
    
    UIImageView*lineImg=[[UIImageView alloc]init];
    lineImg.frame=CGRectMake(0, 39, _bgView.frame.size.width, 1);
    lineImg.backgroundColor=[UIColor lightGrayColor];
    [_bgView addSubview:lineImg];
    
    //取消按钮
    UIButton *confirmBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    confirmBtn.backgroundColor = [UIColor whiteColor];
    confirmBtn.layer.borderWidth = 0.5;
    confirmBtn.layer.borderColor =[UIColor lightGrayColor].CGColor;
    confirmBtn.frame = CGRectMake(0,_bgView.frame.size.height-40, _bgView.frame.size.width/2, 40);
    [confirmBtn setTitle:@"取消" forState:UIControlStateNormal];
    [confirmBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [confirmBtn addTarget:self action:@selector(confirmBtnAction) forControlEvents:UIControlEventTouchUpInside];
    [_bgView addSubview:confirmBtn];
    
    //确定按钮
    UIButton *sure = [UIButton buttonWithType:UIButtonTypeCustom];
    sure.backgroundColor = [UIColor whiteColor];
    sure.layer.borderWidth = 0.5;
    sure.layer.borderColor =[UIColor lightGrayColor].CGColor;
    sure.frame = CGRectMake(_bgView.frame.size.width/2,_bgView.frame.size.height-40, _bgView.frame.size.width/2, 40);
    [sure setTitle:@"确定" forState:UIControlStateNormal];
    [sure setTitleColor:RedColor forState:UIControlStateNormal];
    [sure addTarget:self action:@selector(sure) forControlEvents:UIControlEventTouchUpInside];
    [_bgView addSubview:sure];


    //请求一下 要取消求购的原因
    [Controller requestWithURL:@"AppPurchaseInfo/PostQueryReasonType?from=app" jsondic:nil finishBlock:^(ClassData *classData) {
        if ([classData.number integerValue]==0) {
        NSMutableArray*ary=classData.data;
        //将取消原因和原因的value值 添加到数组
        _dataArray=[[NSMutableArray alloc]init];
        for (NSDictionary*nsDic in ary) {
            TextValue*text=[[TextValue alloc]initWithDic:nsDic];
            [_dataArray addObject:text];
        }
        
        //初始化表格设置数据来源
        _tabelView = [[UITableView alloc] initWithFrame:CGRectMake(0, 40,_bgView.frame.size.width , 272)];
        _tabelView.separatorStyle=UITableViewCellSeparatorStyleNone;
        _tabelView.backgroundColor = [UIColor whiteColor];
        _tabelView.delegate = self;
        _tabelView.dataSource = self;
        [_bgView addSubview:_tabelView];
      }
    else{
        ALERTSHOW(classData.message);
        }
    }];
}
//点击取消按钮执行的方法
- (void)confirmBtnAction
{
    [self removeSelfAction];
}
//点击确定按钮执行的方法
-(void)sure
{
    TextValue*txt=_dataArray[indexRow];
    
    NSString*remark=otherTxt.text;
    if ([txt.textMsg isEqualToString:@"其他"]) {
        if (remark.length==0)
        {
            ALERTSHOW(@"选择其他请填写原因信息");
            return;
        }
    }
    else{
        remark=@"";
    }
    
NSDictionary*dic=@{@"PURCHASE_ID":self.idStr,@"REASON_TYPE":txt.vaule,@"REASON_REMARK":remark};
    [Controller requestWithURL:@"AppPurchaseInfo/PostDeleteRecord?from=app" jsondic:dic finishBlock:^(ClassData *classData) {
        NSInteger integ=[classData.number integerValue];
        if (integ==0){
            NSString*str=@"成功";
            self.SelectBlock(str);
            [self removeSelfAction];
        }
        else{
            ALERTSHOW(classData.message);
        }
    }];
}

//展示出选择器
- (void)showSelfAction
{
    //-216
    [UIView animateWithDuration:0 animations:^{
        _bgView.transform = CGAffineTransformMakeTranslation(0,-266);
    } completion:^(BOOL finished) {
        
    }];
}
//移除选择器
- (void)removeSelfAction
{
    [UIView animateWithDuration:0.2 animations:^{
        _bgView.transform  = CGAffineTransformIdentity;
        
    } completion:^(BOOL finished) {
        
        [self removeFromSuperview];
    }];
}

#pragma mark----------------选择器的代理方法
//返回每行的高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row==_dataArray.count-1) {
        return 80;
    }
    return 40;
}
//返回单元格的个数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArray.count;
}
//表格的协议方法
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell*cell=[[UITableViewCell alloc]init];
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    //选中和不选中的图片
    selectImg=[[UIImageView alloc]init];
    selectImg.frame=CGRectMake(20, 12, 16, 16);
    if (indexRow==indexPath.row) {
        selectImg.image=[UIImage imageNamed:@"ic_radio_btn_checked"];
    }
    else
    {
        selectImg.image=[UIImage imageNamed:@"ic_radio_btn_normal"];
        selectImg.tag=indexPath.row+100;
    }
    //  ic_radio_btn_checked
    [cell addSubview:selectImg];
    //显示原因的详细信息
    messageL=[[UILabel alloc]init];
    //messageL.backgroundColor=[UIColor orangeColor];
    messageL.font=[UIFont systemFontOfSize:16];
    messageL.frame=CGRectMake(40, 10, _bgView.frame.size.width-40 ,20);
    TextValue*textMsg=_dataArray[indexPath.row];
    messageL.text=textMsg.textMsg;
    messageL.textColor=[UIColor grayColor];
    [cell addSubview:messageL];
    //最后一行添加一个输入框
    if (indexPath.row==_dataArray.count-1)
    {
        otherTxt=[[UITextField alloc]init];
        otherTxt.font=[UIFont systemFontOfSize:15];
        otherTxt.borderStyle=UITextBorderStyleRoundedRect;
        otherTxt.placeholder=@"如选其他请再次输入原因";
        otherTxt.frame=CGRectMake(20,40, _bgView.frame.size.width-40, 30);
        otherTxt.delegate=self;
        otherTxt.hidden=hiddenYN;
        [otherTxt addTarget:self action:@selector(keyboardDismiss) forControlEvents:UIControlEventEditingDidEndOnExit];
        [cell addSubview:otherTxt];
    }

    return cell;

}
//选中单元格时执行的方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    indexRow=indexPath.row;
    if (indexPath.row==_dataArray.count-1) {
        hiddenYN=NO;
        [_tabelView reloadData];
    }
    else{
        hiddenYN=YES;
        [_tabelView reloadData];
    }
    

//    UIImageView*image=(UIImageView*)[self viewWithTag:indexPath.row+100];
//    image.image=[UIImage imageNamed:@"ic_radio_btn_checked"];
//    
    //selectImg.image=[UIImage imageNamed:@"ic_radio_btn_checked"];
}
//取消选中单元格时执行的方法
/*
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UIImageView*image=(UIImageView*)[self viewWithTag:indexPath.row+100];
    image.image=[UIImage imageNamed:@"ic_radio_btn_normal"];
}*/

#pragma tap  delegate
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
    CGPoint point = [gestureRecognizer locationInView:gestureRecognizer.view];
    if (CGRectContainsPoint(_bgView.frame, point)) {
        return NO;
    }
    return YES;
}

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    [UIView animateWithDuration:0.25 animations:^{
        _bgView.transform = CGAffineTransformMakeTranslation(0,-420);
    } completion:^(BOOL finished) {
        
    }];
}
- (void)textFieldDidEndEditing:(UITextField *)textField;
{
    [UIView animateWithDuration:0.25 animations:^{
        _bgView.transform = CGAffineTransformMakeTranslation(0,-266);
    } completion:^(BOOL finished) {
        
    }];
}

-(void)keyboardDismiss
{
    
}

@end

@implementation TextValue
-(id)initWithDic:(NSDictionary*)dict
{
    self=[super init];
    if (self) {
        self.textMsg=dict[@"text"];
        self.vaule=dict[@"value"];
    }
    return self;
}
@end



